package com.lmxdawn.api.common.exception;

import com.lmxdawn.api.common.api.IErrorCode;
import lombok.Getter;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * API 异常类
 * <p>
 *
 * @author jy
 * @since 2023-11-24 14:24
 */
@Getter
public class ApiException extends RuntimeException {
    
    private IErrorCode errorCode;
    
    public ApiException(IErrorCode errorCode) {
        super(errorCode.getMsg());
        this.errorCode = errorCode;
    }

    public ApiException(Throwable cause) {
        super(cause);
    }
    
    public ApiException(String message, Throwable cause) {
        super(message, cause);
    }

    /**
     * 以字符串形式获取堆栈跟踪
     *
     * @param throwable 可丢弃
     * @return {@link String}
     */
    public static String getStackTraceAsString(Throwable throwable) {
        StringWriter stringWriter = new StringWriter();
        throwable.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }
    
}
